<template>
  <PageWrapper>
    <BasicTable
      title="型号"
      class="page-search-table"
      @register="registerTable"
      :row-selection="{ onChange: doSelection }"
    >
      <template #toolbar>
        <a-button type="primary" @click="openAdd">新增</a-button>
        <a-button type="primary" danger @click="openDelMulit">删除</a-button>
        <a-button @click="closePage">关闭</a-button>
      </template>
      <template #action="{ record }">
        <TableAction
          :actions="[
            {
              icon: 'clarity:note-edit-line',
              tooltip: '编辑',
              onClick: openEdit.bind(null, record),
            },
            {
              label: '删除型号',
              color: 'error',
              popConfirm: {
                title: `确认删除型号:${record.name}吗？`,
                confirm: handleDelete.bind(null, record),
              },
            },
          ]"
        />
      </template>
    </BasicTable>
    <Dialog @register="registerModel" @success="submitSuccess" :brand_id="brandId" />
    <DriveDialog @register="driveModel" @success="submitSuccess" />
  </PageWrapper>
</template>
<script lang="ts">
  import { defineComponent, reactive, watch } from 'vue';
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { PageWrapper } from '/@/components/Page';
  import { useModal } from '/@/components/Modal';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { tableConfig } from './tableData';
  import Dialog from './dialog.vue';
  import DriveDialog from './drivemodel.vue';
  import { delModel } from '/@/api/sys/gateway/gateway';
  // import { demoListApi } from '/@/api/demo/table';
  export default defineComponent({
    components: { PageWrapper, BasicTable, Dialog, TableAction, DriveDialog },
    props: {
      brandId: {
        type: Number,
      },
    },
    emits: ['closeChange', 'register'],
    setup(props, { emit }) {
      const [registerModel, { openModal: openModal }] = useModal();
      const [driveModel, { openModal: openDriveModal }] = useModal();
      const [registerTable, { reload }] = useTable(tableConfig);
      const { createMessage, createConfirm } = useMessage();
      let delArr: any[] = reactive([]);
      watch(
        () => props.brandId,
        (newVal, oldVal) => {
          if (props.brandId && newVal != oldVal) {
            reload();
          }
        },
      );
      //关闭页面
      function closePage() {
        emit('closeChange');
      }
      function openAdd() {
        openModal(true, { isUpdate: false });
      }
      function openEdit(rowdata: Recordable) {
        openModal(true, {
          rowdata,
          isUpdate: true,
        });
      }
      //删除
      async function handleDelete(rowData) {
        let id = rowData.id;
        await delModel({
          id_list: [id],
        });
        createMessage.success('删除成功');
        reload();
      }

      function submitSuccess(e) {
        reload();
        createMessage.success(e.isUpdate ? '修改成功' : '添加成功');
      }
      function openDriveFrom(rowdata: Recordable) {
        openDriveModal(true, {
          rowdata,
          isUpdate: true,
        });
      }
      async function openDelMulit() {
        createConfirm({
          iconType: 'info',
          title: '确定删除吗',
          onOk() {
            delModel({
              id_list: delArr,
            });
            createMessage.success('删除成功');
            reload();
          },
        });
      }
      function doSelection(selectedRowKeys: []) {
        delArr = [...selectedRowKeys];
      }

      return {
        registerModel,
        openModal,
        registerTable,
        openDriveFrom,
        openAdd,
        openEdit,
        handleDelete,
        openDelMulit,
        doSelection,
        driveModel,
        openDriveModal,
        submitSuccess,
        closePage,
      };
    },
  });
</script>
